/**
 * 
 */
package com.td.framework.webmanager.qxgl.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.td.framework.common.Constant;
import com.td.framework.webmanager.domain.AppsysQxgx;
import com.td.framework.webmanager.domain.AppsysQxxx;
import com.td.framework.webmanager.domain.AppsysQxygns;
import com.td.framework.webmanager.qxgl.dao.QxglDao;

/**
 * 
 * Title:权限处理数据层实现类 Description: 实现数据层权限处理方法 Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * 
 * @author FLT006
 * @version 1.0
 */
public class QxglDaoImpl extends HibernateDaoSupport implements QxglDao {
	private static final Log log = LogFactory.getLog(QxglDaoImpl.class);
	private String sql = "";// sql语句;

	/**
	 * @toDo:保存权限信息;
	 * @param qxxx
	 * @return boolean;
	 */
	public boolean addAppsysQxxx(AppsysQxxx qxxx) {
		// TODO Auto-generated method stub
		boolean flag = false;
		try {
			Session session = getSession();
			//Transaction t = session.beginTransaction();
			session.save(qxxx);
			//t.commit();
			////session.close();
			flag = true;
		} catch (RuntimeException re) {
			flag = false;
			log.error("Exception： QxxxDaoImpl+addAppsysQxxx", re);
			throw re;
		}
		return flag;
	}

	/**
	 * @toDo:保存权限信息;
	 * @param yhzbh
	 * @param ymbhs
	 * @param type
	 * @param qxbh
	 * @param rightList
	 * @return boolean;
	 */
	public boolean addAppsysQxxx(String yhzbh, String[] ymbhs) {
		boolean flag = false;
		Connection con = null;
		Session session = getSession();
		try {
			con = session.connection();
			Statement st = con.createStatement();
			con.setAutoCommit(false);
			// 权限编号为空,直接保存新权限;
			if(yhzbh.split(",").length>=1){
				String[] yhzbhs =  yhzbh.split(",");
				for(String yhzbh1:yhzbhs){
					//删除该用户上的所有页面权限
					sql="delete from appsys_yhzqx where yhzbh = '"+yhzbh1+"' and gncc = 0";
					st.addBatch(sql);
					if(ymbhs!=null){
						for (int i = 0; i < ymbhs.length; i++) {
							sql = "insert into appsys_yhzqx (qxbh,yhzbh,scbz,flag,gncc,xtbm) "
									+ "values ('"+ymbhs[i]+"','"
									+ yhzbh1 + "','0','0','0','"+Constant.SYS_XTBM+"')";					
							st.addBatch(sql);
						}
					}
				}
			}
			st.executeBatch();
			flag = true;
		} catch (SQLException e) {
			flag = false;
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
				log.error("Exception： QxxxDaoImpl+addAppsysQxxx", e1);
			}
			e.printStackTrace();
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				flag = false;
				e.printStackTrace();
				log.error("Exception： QxxxDaoImpl+addAppsysQxxx", e);
			}
		}
		return flag;
	}
	
	/**
	 * @toDo:删除权限信息;
	 * @return boolean;
	 */
	public boolean deleteAppsysQxxx() {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * @toDo:查询权限信息List;
	 * @return list;
	 */
	public List<AppsysQxxx> fingby() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @toDo:通过用户组编号得到对应的权限编号;
	 * @param yhzbh
	 * @param flag
	 * @return qxbh;
	 */
	@SuppressWarnings("unchecked")
	public Long getQxbh(String yhzbh, Long flag) {
		Long qxbh = new Long(0);
		sql = "select t.qxbh from com.td.framework.webmanager.domain.AppsysYhzqx t " +
				"where t.xtbm ='"+Constant.SYS_XTBM+"' and t.yhzbh='"
				+ yhzbh + "' and t.flag=" + flag;
		List list = this.getHibernateTemplate().find(sql);
		if (!list.isEmpty()) {
			qxbh = (Long) list.get(0);
		}
		return qxbh;

	}
	
	/**
	 * @toDo:通过用户/用户组编号查询权限编号;
	 * @param yhbhs
	 * @return qxbhs;
	 */
	@SuppressWarnings("unchecked")
	public String getQxbh(String yhbhs)
	{
		StringBuffer qxbhs = new StringBuffer("");
		if(!yhbhs.equals(""))
		{
			sql = "select t.qxbh from com.td.framework.webmanager.domain.AppsysYhzqx t " +
					"where t.scbz=0 and t.xtbm ='"+Constant.SYS_XTBM+"'";
			String condition = " and ";
			String temp = "";
			for(int i=0; i<yhbhs.split(",").length;i++)
			{
				temp=yhbhs.split(",")[i];
				if(i==yhbhs.split(",").length-1)
				{
					condition +=" (t.yhzbh='"
						+ temp.split("/")[0] + "' and t.flag=" + temp.split("/")[1]+" ) ";
				}
				else
				{
					condition +=" (t.yhzbh='"
						+ temp.split("/")[0] + "' and t.flag=" + temp.split("/")[1]+" ) or ";
				}

			}
			sql +=condition;
			List<Long> list = this.getHibernateTemplate().find(sql);
			for (int i=0;i<list.size();i++) {
				if(i==list.size()-1)
				{
					qxbhs.append(list.get(i));
				}
				else
				{
					qxbhs.append(list.get(i)+",");
				}
			}
			
		}
		return qxbhs.toString();
		
	}
	/**
	 * @toDo:查询权限信息;
	 * @return AppsysQxxx;
	 */
	public AppsysQxxx getAppsysQxxx() {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @toDo: 更新权限信息;
	 * @param qxxx
	 * @return boolean;
	 */
	public boolean updateAppsysQxxx(AppsysQxxx qxxx) {
		// TODO Auto-generated method stub
		return false;
	}
	/**
	 * @toDo: 根据页面编号、功能编号、功能类型读取权限关系;
	 * @param ymbh
	 * @param gnbh
	 * @param qxbz
	 * @return appsysQxgx
	 */
	@SuppressWarnings("unchecked")
	public AppsysQxgx getAppsysQxgx(String ymbh,String gnbh,int qxbz)
	{
		AppsysQxgx appsysQxgx =null;
		sql = "from AppsysQxgx t" +
				" where t.ymbh='"+ymbh+"' and lxbhz="+gnbh+" and t.qxbz="+qxbz;
		List<AppsysQxgx> list =this.getHibernateTemplate().find(sql);
		if(list!=null&&!list.isEmpty())
		{
			appsysQxgx = list.get(0);
		}
		return appsysQxgx;
	}
}
